﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VocabLib;

namespace VocabApp
{
	class Program
	{
        private static VocabModel model;

		static void Main( string[] args )
		{
            model = new VocabModel();
            ConsoleLoop();
		}

        private static void ConsoleLoop<TAsked>(Func<TAsked> askFunc, Func<string, TAsked, bool> parseFunc)
        {
            while (true)
            {
                TAsked asked = askFunc();
                string fullComm = Console.ReadLine();
                bool continueLoop = parseFunc(fullComm, asked);

                if (!continueLoop)
                {
                    break;
                }
            }
        }

        public static void ConsoleLoop()
        {
            ConsoleLoop(() =>
            {
                Console.WriteLine("Commands: load/l, guess/g, exit/e");
                return 1;
            }, ParseCommand);
        }

        private static bool ParseCommand(string fullComm, int dontCare)
        {
            string[] tokens = fullComm.Split(' ');
            string comm = tokens[0];

            switch (comm)
            {
                case "load":
                case "l":
                    string path;
                    if (tokens.Length < 2)
                    {
                        path = @"D:\Download\Dropbox\Angol\koviszotar-utf8.csv";
                    }
                    else
                    {
                        path = tokens[1];
                    }
                    model.MergeDictionary(path);
                    break;
                case "guess":
                case "g":
                    Guess();
                    break;
                case "exit":
                case "e":
                    return false;
            }

            return true;
        }

        private static void Guess()
        {
            ConsoleLoop(AskRecord, ParseGuess);
        }

        private static VocabLib.VocabModel.Record AskRecord()
        {
            var rec = model.GetNextQuestion();
            Console.WriteLine("\"" + rec.Hun + "\"" + " in English?");

            return rec;
        }

        private static bool ParseGuess(string guess, VocabLib.VocabModel.Record askedRecord)
        {
            bool success = askedRecord.Guess(guess);

            // Put back with new priority

            if (success)
            {
                Console.WriteLine("Correct answer!");
            }
            else
            {
                Console.WriteLine("Wrong answer, the correct answer would have been \"" + askedRecord.Eng + "\"");
            }

            return true;
        }
	}
}
